<template>
  <div class="account-settings-info-view">
    <Row :gutter="16">
      <Col :md="24" :lg="16">
      <Form ref="userForm" :rules="formItemRules" :model="profile">
        <FormItem label="昵称" prop="nickName">
          <Input v-model="profile.nickName" placeholder="请输入内容"></Input>
        </FormItem>
        <FormItem label="描述">
          <Input v-model="profile.userDesc" type="textarea" placeholder="请输入内容"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="handleSubmit('userForm')">保存</Button>
        </FormItem>
      </Form>
      </Col>
      <Col :md="24" :lg="8" :style="{ minHeight: '180px' }">
      <div class="ivu-upload-preview" @click="$refs.modal.edit(1)">
        <Icon type="ios-cloud-upload-outline" class="upload-icon"/>
        <div class="mask">
          <Icon type="ios-add"/>
        </div>
        <img :src="option.img"/>
      </div>
      </Col>

    </Row>

    <avatar-modal ref="modal">

    </avatar-modal>
  </div>
</template>

<script>
import AvatarModal from './AvatarModal'

export default {
  components: {
    AvatarModal
  },
  data () {
    return {
      formItemRules: {
        nickName: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ]
      },
      profile: this.$store.state.user.info,
      // cropper
      preview: {},
      option: {
        img: '/avatar.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      }
    }
  },
  methods: {
    handleSubmit (form) {
      if (form === 'userForm') {
        this.$refs['userForm'].validate((valid) => {
          if (valid) {
            this.$http.post(this.$apis.user.updateCurrent, this.profile, this).then(res => {
              if (res.code === 0) {
                this.$store.commit('SET_NAME', { name: this.profile.nickName })
                this.$store.commit('SET_INFO', this.profile)
                this.$Message.success('更新成功')
              }
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ivu-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }

    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0, 0, 0, 0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
